## Load required packages

require(foreign)
require(pscl)

## Load raw roll call data supplemented with interest group positions;
## These were downloaded separately from www.voteview.com


h107IG <- read.dta("hou107_IGs.dta")
s107IG <- read.dta("sen107_IGs.dta")

## create roll call object for analysis by ideal within pscl
## columns correspond to the peacetime and wartime roll call votes

h107.IG.pre <- rollcall(h107IG[,10:369],yea=c(1,2,3), nay=c(4,5,6), missing=c(7,8,9), notInLegis=0, legis.names=t(h107IG[,9]))
h107.IG.post <- rollcall(h107IG[,370:ncol(h107)],yea=c(1,2,3), nay=c(4,5,6), missing=c(7,8,9), notInLegis=0, legis.names=t(h107IG[,9]))

s107.IG.pre <- rollcall(s107IG[,9:300],yea=c(1,2,3), nay=c(4,5,6), missing=c(7,8,9), notInLegis=0, legis.names=t(s107IG[,8]))
s107.IG.post <- rollcall(s107IG[,301:ncol(s107)],yea=c(1,2,3), nay=c(4,5,6), missing=c(7,8,9), notInLegis=0, legis.names=t(s107IG[,8]))



###########################
## ESTIMATE IDEAL POINTS ##
###########################

###########
## HOUSE ##
###########

h107.ideal.IG.pre <- ideal(h107.IG.pre,normalize=TRUE,maxiter = 350000, thin = 500, burnin = 50000,store.item=TRUE)
h107.ideal.IG.post <- ideal(h107.IG.post, maxiter = 350000, thin = 500, burnin = 50000,store.item=TRUE)

############
## SENATE ##
############

s107.ideal.IG.pre <- ideal(s107.IG.pre, normalize=TRUE,maxiter = 350000, thin = 500, burnin = 50000,store.item=TRUE)
s107.ideal.IG.post <- ideal(s107.IG.post, maxiter = 350000, thin = 500, burnin = 50000,store.item=TRUE)




#############
## BRIDGES ##
#############

###########
## HOUSE ##
###########

## post-process the estimates so that the ACU and ADA have the same peacetime and wartime ideal points
h107.pp.IG.pre <- postProcess(h107.ideal.IG.pre, constraints=list("ACU"=1.575,"ADA"=-1.375))
h107.pp.IG.post <- postProcess(h107.ideal.IG.post, constraints=list("ACU"=1.575,"ADA"=-1.375))
t.test(h107.pp.IG.pre$xbar, h107.pp.IG.post$xbar,pair=T,alt="l")

## Bridging can now also be done using the member bridges, setting their wartime estimate equal to their peacetime estimate


############
## SENATE ##
############

## post-process the estimates so that the ACU and ADA have the same peacetime and wartime ideal points
s107.pp.IG.pre <- postProcess(s107.ideal.IG.pre, constraints=list("ACU"=1.584,"ADA"=-.814))
s107.pp.IG.post <- postProcess(s107.ideal.IG.post, constraints=list("ACU"=1.584,"ADA"=-.814))
t.test(s107.pp.IG.pre$xbar, s107.pp.IG.post$xbar,pair=T,alt="l")

## Bridging can now also be done using the member bridges, setting their wartime estimate equal to their peacetime estimate